<template>
  <div class="login" id="app">
    <el-card shadow="hover">
      <el-form :rules="rules" status-icon :model="form" ref="form">
        <div class="title"></div>
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-unlock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="form.repassword"
            placeholder="请确认密码"
            prefix-icon="el-icon-unlock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit(form)">注册</el-button>
        </el-form-item>
        <el-link @click="$router.push('/login')">去登录</el-link>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { regAPI } from '../axios/api.js'
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        username: 'lijianyu',
        password: '123456',
        repassword: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '字母数字、长度 1-10', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请设置密码', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{6,10}$/, message: '非空字符串、长度 6-15', trigger: 'blur' }
        ],
        repassword: [
          { validator: validatePass, trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{6,10}$/, message: '非空字符串、长度 6-15', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit(form) {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 发送axios请求
          const { data: res } = await regAPI(form)
          if (res.code === 0) {
            // 注册成功
            // 显示成功信息
            this.$message({
              showClose: true,
              message: res.message,
              type: 'success'
            })
            // 把用户名存上
            this.$store.commit('user/updataUsername', this.form.username)
            // 跳转到登陆页面
            this.$router.push('/login')
          } else {
            // 注册出现其他问题
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            })
          }
          console.log(res)
          // 表单完整性通过
        } else {
          this.$message({
            showClose: true,
            message: '请正确填写信息',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style lang="less">
.login {
  height: 100%;
  background: url('../assets/images/login_bg.jpg');
  background-size: cover;
  .el-card {
    width: 400px;
    // height: 400px;
    background-color: #fff;
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding-top: 0;
    .title {
      width: 100%;
      height: 50px;
      background: url('../assets/images/login_title.png');
      background-position: top center;
      background-repeat: no-repeat;
    }
    .el-button {
      width: 100%;
    }
  }
}
</style>
